import Vue from 'vue'
import VueRouter from 'vue-router'
/*登录界面*/
import Login from '../views/login/index'
/*后台框架*/
import Layout from '../views/layout/index'
/*主页*/
import Home from '../views/home/index'
/*用户管理*/
import User from '../views/user/index'
import AddUser from '../views/user/components/addUser'
import EditUser from '../views/user/components/editUser'
/*角色管理*/
import Role from '../views/role/index'
import AddRole from '../views/role/components/addRole'
import EditRole from '../views/role/components/editRole'
/*权限管理*/
import Permission from '../views/permission/index'
import AddPermission from '../views/permission/components/addPermission'
import EditPermission from '../views/permission/components/editPermission'
/*文章管理*/
import Article from '../views/article/index'
import EditArticle from '../views/article/components/editArticle'
import CheckArticle from '../views/article/components/checkArticle'
import Publish from '../views/publish/index'
/*文章分类管理*/
import ArticleChannel from '../views/article_channel/index'
/*素材管理*/
import Image from '../views/image/index'
/*个人设置*/
import Personal from '../views/personal/index'
/*图表管理*/
import DemoChart from '../views/charts/demo'
/*登录日志管理*/
import LoginLog from '../views/login_log/index'
/*操作日志管理*/
import SystemLog from '../views/system_log/index'
/*推送中心*/
import WebSocketIndex from '../views/socket/index'
/*404*/
import NoPage from '../views/error/404'

/*分类管理*/
import Category from '../views/category/index'

/*进度条*/
import 'nprogress/nprogress.css'
import NProgress from 'nprogress'

Vue.use(VueRouter)

//配置进度条动画效果
NProgress.configure({ease: 'ease', speed: 500})

const routes = [
    {
        path: '/login',
        name: 'login',
        component: Login
    },
    {
        path: '/',
        // name: 'layout',
        component: Layout,
        children: [
            {
                path: '',
                name: 'home',
                component: Home
            },
            {
                path: '/category/list/tree',
                name: 'category',
                component: Category
            },
            {
                path: '/admin/push/notify',
                name: 'webSocketIndex',
                component: WebSocketIndex
            },
            {
                path: '/queryArticleByCondition',
                name: 'article',
                component: Article
            },
            {
                path: '/editArticle',
                name: 'editArticle',
                component: EditArticle
            },
            {
                path: '/checkArticle',
                name: 'checkArticle',
                component: CheckArticle
            },
            {
                path: '/queryUserByCondition',
                name: 'user',
                component: User
            },
            {
                path: '/addUser',
                name: 'addUser',
                component: AddUser
            },
            {
                path: '/editUser',
                name: 'editUser',
                component: EditUser
            },
            {
                path: '/queryRoleByCondition',
                name: 'role',
                component: Role
            },
            {
                path: '/addRole',
                name: 'addRole',
                component: AddRole
            },
            {
                path: '/editRole',
                name: 'editRole',
                component: EditRole
            },
            {
                path: '/queryPermissionByCondition',
                name: 'permission',
                component: Permission
            },
            {
                path: '/addPermission',
                name: 'addPermission',
                component: AddPermission
            },
            {
                path: '/editPermission',
                name: 'editPermission',
                component: EditPermission
            },
            {
                path: '/publish',
                name: 'publish',
                component: Publish
            },
            {
                path: '/getAll/articleChannel',
                name: 'articleChannel',
                component: ArticleChannel
            },

            {
                path: '/images',
                name: 'image',
                component: Image
            },
            {
                path: '/personal-setting',
                name: 'personal',
                component: Personal
            },
            {
                path: '/demo',
                name: 'demoChart',
                component: DemoChart
            },
            {
                path: '/queryLoginLogByCondition',
                name: 'loginLog',
                component: LoginLog
            },
            {
                path: '/querySystemLogByCondition',
                name: 'systemLog',
                component: SystemLog
            },
            {
                path: '/404',
                name: '404',
                component: NoPage
            },
            {
                path: "*",
                redirect: "/404"
            }

        ]
    },

]

const router = new VueRouter({
    mode: 'hash',
    routes
})

// router 前置导航,页面拦截
/**
 * to:  跳转的router
 * from:当前的router
 * next:放行router
 */
router.beforeEach((to, from, next) => {
    // NProgress开始进度条
    NProgress.start()
    // 判断跳转的router是不是登录页面/login 如果不是登录页面，则判断是否登录
    if(to.path !== '/login'){
        // 非登录页面，判断登录状态
        const token =  window.localStorage.getItem('ww-token')
        if(token){
            // 已登录
            next()
            NProgress.done()
        }else{
            // 未登录，跳转登录页面
            next('/login')
            NProgress.done()
        }
    }else{
        // 登录页面放行
        next()     // 放行
        NProgress.done()
    }
});

router.afterEach(() => {
    NProgress.done()
});


export default router
